package com.vlong.town.controller.admin.policy;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.policy.PolicyInfo;
import com.vlong.town.service.policy.PolicyInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 政策规定Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/policy")
public class PolicyInfoController extends BaseController {

    // Fields

    @Autowired
    private PolicyInfoService policyInfoService;// 政策规定业务实现类

    /**
     * 进入政策规定列表页面
     * @return policy/list_policy
     */
    @RequestMapping("/list")
    public String gotoPolicyInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<PolicyInfo> policyInfoList = this.policyInfoService.queryList(title, content, desc);
        PageInfo<PolicyInfo> pageInfo = new PageInfo<PolicyInfo>(policyInfoList);
        PageTag<PolicyInfo> pt = new PageTag<PolicyInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(PolicyInfo policyInfo : policyInfoList) {
            policyInfo.setContent(DataUtil.delHTMLTag(policyInfo.getContent(), 0));
        }
        request.setAttribute("policyInfoList", policyInfoList);
        return "admin/policy/list_policy";
    }

    /**
     * 进入政策规定添加页面
     * @return policy/add_policy
     */
    @GetMapping("/addPolicy")
    public String gotoPolicyInfoAddPage() {
        return "admin/policy/add_policy";
    }

    /**
     * 进入政策规定修改页面
     * @return policy/edit_policy
     */
    @GetMapping("/editPolicy/{policyId}")
    public String gotoPolicyInfoEditPage(@PathVariable(required=true, value="policyId") Long policyId) {
        PolicyInfo policyInfo = this.policyInfoService.queryById(policyId);
        request.setAttribute("policyInfo", policyInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + policyInfo.getThumbnail());
        return "admin/policy/edit_policy";
    }

    /**
     * 添加政策规定
     * @param title 政策标题
     * @param content 政策内容
     * @param desc 政策简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/executeAddPolicy")
    public DataMessage addPolicy(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("政策标题不合法");
            return dm;
        }
        this.policyInfoService.add(1L, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("政策规定添加成功");
        return dm;
    }

    /**
     * 修改政策规定
     * @param title 政策标题
     * @param content 政策内容
     * @param desc 政策简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditPolicy")
    public DataMessage editPolicy(
            @RequestParam(name="policyId", required=true) Long policyId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("政策标题不合法");
            return dm;
        }
        this.policyInfoService.update(policyId, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("政策修改成功");
        return dm;
    }

    /**
     * 删除政策
     * @param policyId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="policyId") Long policyId) {
        DataMessage dm = new DataMessage();
        if(policyId != null && policyId > 0L) {
            this.policyInfoService.delete(policyId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("政策删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除政策
     * @param policyIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="policyIds") String policyIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(policyIds, 1, 255)) {
            String[] policyIdArray = policyIds.split(",");
            for(String policyId : policyIdArray) {
                this.policyInfoService.delete(Long.parseLong(policyId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("政策规定删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
